import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

import '../../services/ScreenAdapter.dart';
import '../../widget/MyButton.dart';
import '../../widget/LoadingWidget.dart';
import 'ProductContentFirst.dart';
import 'ProductContentSecond.dart';
import 'ProductContentThird.dart';

// 广播
import '../../services/EventBus.dart';

// 商品详情页
class ProductContentPage extends StatefulWidget{
  final Map arguments;
  ProductContentPage({this.arguments});
  @override
  State<StatefulWidget> createState() => ProductContentPageState(arguments:arguments);
}

class ProductContentPageState extends State<ProductContentPage>{
  Map contentObj;
  bool isLoading = true;
  final Map arguments;
  ProductContentPageState({this.arguments});

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          toolbarHeight: ScreenAdapter.height(130),
          title:Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: ScreenAdapter.width(440),
                child: TabBar(
                  indicatorColor: Colors.red,
                  indicatorSize: TabBarIndicatorSize.label,
                  tabs: [
                    Tab(
                      child: Text('商品'),
                    ),
                    Tab(
                      child: Text('详情'),
                    ),
                    Tab(
                      child: Text('评价'),
                    ),
                  ],
                ),
              ),
            ],
          ),
          actions: [
            IconButton(
              icon: Icon(Icons.more_horiz),
              onPressed: () {
                showMenu(
                    position: RelativeRect.fromLTRB(500, 76, 10, 0),
                    context: context,
                    items: [
                      PopupMenuItem(
                        child: Row(
                          children: [
                            Icon(Icons.home),
                            Text('首页')
                          ],
                        ),
                      ),
                      PopupMenuItem(
                        child: Row(
                          children: [
                            Icon(Icons.search),
                            Text('搜索')
                          ],
                        ),
                      ),
                    ],
                );
              },
            ),
          ],
        ),
        body: !isLoading ? Stack(
          children: [
            TabBarView(
              children: [
                ProductContentFirst(contentObj: this.contentObj),
                ProductContentSecond(contentObj: this.contentObj),
                ProductContentThird(),
              ],
            ),
            Positioned(
              width: ScreenAdapter.width(750),
              height: ScreenAdapter.height(100),
              bottom: 0,
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border(
                        top: BorderSide(
                          color: Colors.black12,
                        )
                    )
                ),
                child: Row(
                  children: [
                    Expanded(
                        flex:1,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            InkWell(
                              child: Icon(Icons.shopping_cart),
                              onTap: (){
                                Navigator.pushNamed(context, '/cart');
                              },
                            ),
                            Text('购物车',style:TextStyle(fontSize: 12)),
                          ],
                        )
                    ),
                    Expanded(
                      flex: 1,
                      child: MyButton(
                        color: Colors.red,
                        text: '加入购物车',
                        cb: (){
                          // 广播
                          eventBus.fire(ProductContentEvent('加入购物车'));
                        },
                      ),
                    ),
                    Expanded(
                      flex: 1,
                      child:  MyButton(
                        color: Colors.orange,
                        text: '立即购买',
                        cb: (){
                          // 广播
                          eventBus.fire(ProductContentEvent('立即购买'));
                        },
                      ),
                    ),
                  ],
                ),
              ),
            )
          ],
        ) : LoadingWidget(),
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    //根据id获取详情数据
    _getContentData();
  }

  //根据id获取详情数据
  _getContentData() async {
    var api = 'http://jd.itying.com/api/pcontent?id=${this.arguments['id']}';
    var data = await Dio().get(api);
    var res = data.data['result'];
    this.setState(() {
      contentObj = res;
      isLoading = false;
    });
  }
}
